package mdt.action;

import org.tigris.gef.base.Globals;
import mdt.figure.FigTextEditorMDT;

import javax.swing.*;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import java.awt.event.ActionEvent;

/**
 * Created by xiaobyu on 7/14/2016.
 */
public class AlignCenterAction extends AbstractAction {
    public AlignCenterAction() {
        super("Align Center");
        putValue(Action.SMALL_ICON, new ImageIcon(FontBoldAction.class.getResource("/resources/align_center-16.png")));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        FigTextEditorMDT te = (FigTextEditorMDT) Globals.curEditor().getActiveTextEditor();
        if (te == null) return;
        int selStart = te.getSelectionStart();
        int selEnd = te.getSelectionEnd();
        if (selEnd <= selStart) return;

        StyledDocument doc = te.getStyledDocument();
        Style def = StyleContext.getDefaultStyleContext().
                getStyle(StyleContext.DEFAULT_STYLE);
        Style s = doc.addStyle("bold", def);
        StyleConstants.setBold(s, true);
        doc.setCharacterAttributes(selStart, selEnd - selStart, s, true);
    }
}
